package com.feicheng.controller.system;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PutMapping;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Category;
import com.feicheng.persistence.entity.Product;
import com.feicheng.service.IFProductService;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2022-01-10
 */
@RestController("/admin/product")
@RequestMapping("/admin/product")
public class ProductController {
	
	@Autowired
	IFProductService productService;
	
	//详细查询分类信息
	@GetMapping("/category/info")
	public ResultInfo getCategoryDetail(Integer categoryId) {
		
		if (null == categoryId) {
			throw new SystemException("分类主键缺失");
		}
		
		return productService.getCategoryDetail(categoryId);
	}
	
	//分页查询分类信息
	@GetMapping("/category/page")
	public ResultInfo getCategorysByPage(
			Category category, 
			@RequestParam(defaultValue="1") Integer current, 
			@RequestParam(defaultValue="10")Integer size) {
		
		return productService.getCagetorysByPage(category, current, size);
		
	}
	
	//新增分类信息
	@PostMapping("/category/new")
    public ResultInfo newCategoryInfo(Category category, String url, String contant) {
		if ( category.getName() == null
	        || category.getPId() == null
	        || category.getLevel() == null
	        || category.getStatu() == null
	        || url == null
	        || contant == null
	        ) {
			throw new SystemException("分类信息缺失");
        }
        return productService.newCategoryInfo(category,url,contant);
    }
	
	//删除分类信息
	@DeleteMapping("/category/info")
	public ResultInfo deleteCategoryInfo(Integer categoryId) {
		if(null == categoryId){
            throw new SystemException("分类ID缺失");
        }
		return productService.deleteCategoryInfo(categoryId);
	}
	
	//详细查询产品信息
	@GetMapping("/info")
	public ResultInfo getProductDetail(Integer productId) {
		
		if (null == productId) {
			throw new SystemException("产品ID缺失");
		}
		return productService.getProductDetail(productId);
	}
	
	//删除产品信息
	@DeleteMapping("/info")
	public ResultInfo deleteProductInfo(Integer productId) {
		if(null == productId){
            throw new SystemException("产品ID缺失");
        }
		return productService.deleteProductInfo(productId);
	}
	
	//产品管理分页查询接口
		@GetMapping("/product/page")
		public ResultInfo getProductsByPage(
				Product product, 
				@RequestParam(defaultValue="1") Integer current, 
				@RequestParam(defaultValue="10")Integer size,
				Integer endPrice) {
			
			return productService.getProductsByPage(product,current,size,endPrice);
			
		}
		
		//产品管理修改接口
		@PutMapping("/product/info")
		public ResultInfo updateProduct(
				Product product,
				String url,
				Boolean imgStatu,
				Boolean ImgDflg,
				String josnArrayUrl) {
			
			
			JSONArray jsonArrayUrl = JSONObject.parseArray(josnArrayUrl);
			
			
			 if (product.getProductId() == null) { throw new SystemException("丢失主键"); } 
			 if (product.getCategorys() == null) { throw new SystemException("丢失父类"); } 
			 if (product.getName() == null) { throw new SystemException("丢失名称"); }
			return productService.updateProduct(product,url,imgStatu,ImgDflg, jsonArrayUrl);
		}
		
		//查询分类level个数
		@GetMapping("/level")
		public ResultInfo levelNum() {
			return productService.getLevelNum();
		}
		
		//查询父级分类信息
		@GetMapping("/pInfo")
		public ResultInfo pInfo(Integer level) {
			if(null == level){
	            throw new SystemException("分类等级缺失");
	        }
			return productService.getPList(level);
		}
		
		//根据分类编号查询父级分类名称
		@GetMapping("/pName")
		public ResultInfo pName(Integer categoryId) {
			if(null == categoryId){
			    throw new SystemException("分类id缺失");
			}
				return productService.getPName(categoryId);
		}
	
		//修改分类详细信息
		@PutMapping("/category/cInfo")
		public ResultInfo updateCategoryInfo(Category category, String url, String contant) {
			
			if(null == category.getCategoryId()){
	            throw new SystemException("分类ID缺失");
	        }
			
			return productService.updateCategoryInfo(category, url, contant);
		}
		
		//新增产品管理信息
				@PostMapping("/info")
				public ResultInfo addProductInfo(Product product, String url, String urlS){
					
					if(null == product.getName()){
			            throw new SystemException("产品缺失");
			        }
//					if(null == urlS) {
//						throw new SystemException("产品详细图片缺失");
//					}
					JSONArray urls = JSONObject.parseArray(urlS);
					return productService.addProductInfo(product, url, urls);
				}
}
